<?php

function update_user($redirect = true, $firsttime = false){
    $CI =& get_instance();
    $UserInfo = $CI->session->userdata('UserInfo');
    $query = $CI->db->query(""
            . "Select * "
            . "From ".MYSQL_TABLE_PREFLIX.TABLE_USER_ACCOUNTS." "
            . "Where UserEmail = '".$UserInfo['UserEmail']."' AND UserPassword = "."'".$UserInfo['UserPassword']."'") ;
    $query = $query->result_array(); 
    if($query){
        $UserInfo = $query[0];
        $CI->session->set_userdata('UserInfo', $UserInfo);    
    }
    else{
        $CI->session->unset_userdata('UserInfo');   
        return 0;
    }
    
    if($UserInfo['UserType'] == 'work'){
        $query = $CI->db->get_where(MYSQL_TABLE_PREFLIX.TABLE_USER3_ACCOUNTS_INFORMATION,
                array('RelUserEmail' => $UserInfo['UserEmail']));
        if($query->num_rows == 0){
            if(!$firsttime)
                redirect(site_url());
        }
        else{
            $temp = $query->result_array();
            $UserInfo = array_merge($UserInfo,$temp[0]);
            $CI->session->set_userdata('UserInfo', $UserInfo);    
        }
        return 3;
    }
    elseif($UserInfo['UserType'] == 'hire'){
        $query = $CI->db->get_where(MYSQL_TABLE_PREFLIX.TABLE_USER2_ACCOUNTS_INFORMATION,
                array('RelUserEmail' => $UserInfo['UserEmail']));
        if($query->num_rows == 0){
            if(!$firsttime)
                redirect(site_url());
        }
        else{
            $temp = $query->result_array();
            $UserInfo = array_merge($UserInfo,$temp[0]);
            $CI->session->set_userdata('UserInfo', $UserInfo);    
        }
        return 2;
    }
    else{
        if($redirect)
            redirect($site_url);
    }
}





function user_avatar_link(){
    $CI =& get_instance();
    $UserInfo = $CI->session->userdata('UserInfo');

    if($UserInfo['UserType'] == 'hire'){
        $query = $CI->db->get_where(MYSQL_TABLE_PREFLIX.TABLE_USER2_ACCOUNTS_INFORMATION,
                            array('RelUserEmail' => $UserInfo['UserEmail']));
    }
    elseif($UserInfo['UserType'] == 'work'){
        $query = $CI->db->get_where(MYSQL_TABLE_PREFLIX.TABLE_USER3_ACCOUNTS_INFORMATION,
                            array('RelUserEmail' => $UserInfo['UserEmail']));
    }

    if(isset($querry) || $query->num_rows == 0){
        return image_url().'no-avatar.png';
    }
    else{
        $query = $query->result_array();
        $query = $query[0];
        return $query['AvatarLink'];
    }
}